﻿using System;
using System.Linq;
using System.Windows.Controls;
using PhoneAppRssReader.Entity;

namespace PhoneAppRssReader
{
    public partial class MainPage
    {

        // Constructor
        public MainPage()
        {
            InitializeComponent();
            DataContext = App.Source;
            Loaded += (sender, e) =>
                          {
                              if (!App.Source.IsDataLoad)
                                  App.Source.LoadData();
                              if (!App.Source.Items.Any())
                              {
                                  NavigationService.Navigate(new Uri("/Setup.xaml", UriKind.Relative));
                              }
                          };

        }



        private void itemsListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var listBox = sender as ListBox;
            if (listBox == null) return;
            if (listBox.SelectedIndex == -1) return;

            var feedData = listBox.Items[listBox.SelectedIndex] as Feed;
            if (feedData != null)
                NavigationService.Navigate(
                        new Uri(string.Format("/FeedDataPage.xaml?id={0}", feedData.Id),
                                UriKind.RelativeOrAbsolute));

        }

        private void Refresh(object sender, EventArgs e)
        {
        	// TODO: Add event handler implementation here.
            //Clear items and reload
            App.Source.Items.Clear();
            App.Source.LoadData();
        }

      
        private void Setting(object sender, EventArgs e)
        {
        	// TODO: Add event handler implementation here.
			 NavigationService.Navigate(new Uri("/ManagerRssFeedPage.xaml", UriKind.Relative));
        }

    }
}